"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
const taro_1 = require("@tarojs/taro");
const components_1 = require("@tarojs/components");
const redux_1 = require("@tarojs/redux");
const taro_ui_1 = require("taro-ui");
const config_1 = require("../../config");
const goods_1 = require("../../actions/goods");
require("./index.scss");
const IMAGE_PREFIX = 'https://clover-mini-program.oss-cn-beijing.aliyuncs.com/';
let Activity = class Activity extends taro_1.Component {
    constructor() {
        super(...arguments);
        this.config = {
            navigationBarTitleText: '幸运草小程序'
        };
        this.state = {
            title: '',
            explain: '',
            date: '请选择开奖日期',
            time: '请选择开奖时间',
            isOpened: false,
            isToastOpened: false,
            data: {
                GoodsPrizes: [
                    {
                        number: 1,
                        cover: ''
                    }
                ],
                brief: '',
                style: -1,
                number: 0,
                date: '',
                time: ''
            }
        };
        this.handleAddComponent = () => {
            this.setState(state => (Object.assign({}, state, { isToastOpened: true })));
        };
        this.handleImageUpload = () => {
            taro_1.default.getSetting().then(e => {
                if (e.authSetting['scope.userInfo']) {
                    taro_1.default.chooseImage({
                        count: 1,
                        sizeType: 'compressed'
                    }).then(results => {
                        const { tempFilePaths = [] } = results;
                        taro_1.default.uploadFile({
                            url: `${config_1.host}/api/v1/uploads`,
                            filePath: tempFilePaths[0],
                            name: 'file',
                        }).then(results => {
                            const data = JSON.parse(results.data);
                            taro_1.default.showToast({ title: `奖品图片上传成功~`, icon: 'success' });
                            this.setState(state => (Object.assign({}, state, { data: Object.assign({}, state.data, { GoodsPrizes: [Object.assign({}, state.data.GoodsPrizes[0], { cover: data.data.name })] }) })), () => console.log(this.state, 'state'));
                        });
                    }).catch(() => taro_1.default.showToast({ title: `图片上传失败`, icon: 'none' }));
                }
                else {
                    taro_1.default.showToast({ title: `请先登录！`, icon: 'none' });
                }
            });
        };
        this.handleSubmits = () => {
            taro_1.default.getSetting().then(e => {
                if (e.authSetting['scope.userInfo']) {
                    const { GoodsPrizes, number, nickname, brief, userId, style, date, time } = this.state.data;
                    const { title, cover } = GoodsPrizes[0];
                    if (style === -1)
                        return taro_1.default.showToast({ title: `请选择开奖条件！`, icon: 'none' });
                    if (!date && !number)
                        return taro_1.default.showToast({ title: `按人数或按日期时间开奖至少选填一项！`, icon: 'none' });
                    if (!title)
                        return taro_1.default.showToast({ title: `奖品名称不能为空！`, icon: 'none' });
                    if (!cover)
                        return taro_1.default.showToast({ title: `奖品图片不能为空！`, icon: 'none' });
                    const obj = {};
                    if (date && time)
                        Object.assign(obj, { endAt: date + ' ' + time });
                    // console.log({...this.state.data, ...obj}, '123321')
                    this.props.add(Object.assign({}, this.state.data, obj), (data) => {
                        if (data.errno) {
                            taro_1.default.showToast({ title: `奖品添加失败~`, icon: 'none' });
                        }
                        else {
                            taro_1.default.showToast({ title: `奖品添加成功~`, icon: 'success' });
                        }
                    });
                }
                else {
                    taro_1.default.showToast({ title: `请先登录！`, icon: 'none' });
                }
            });
        };
        this.handleInputChange = title => {
            this.setState(state => (Object.assign({}, state, { data: Object.assign({}, state.data, { GoodsPrizes: [Object.assign({}, state.data.GoodsPrizes[0], { title })] }) })), () => console.log(this.state, '123'));
        };
        this.handleNumberChange = number => {
            this.setState(state => (Object.assign({}, state, { data: Object.assign({}, state.data, { GoodsPrizes: [Object.assign({}, state.data.GoodsPrizes[0], { number })] }) })), () => console.log(this.state, '123'));
        };
        this.handleTextareaChange = e => {
            e.stopPropagation();
            const value = e.target.value;
            this.setState(state => (Object.assign({}, state, { data: Object.assign({}, state.data, { brief: value }) })), () => console.log(this.state, '123'));
        };
        this.handleActionSheetSelect = e => {
            e.stopPropagation();
            this.setState(state => (Object.assign({}, state, { isOpened: true })));
        };
        this.handleDateTimeChange = (flag, e) => {
            e.stopPropagation();
            const value = e.target.value;
            let data = {};
            if (flag === 1)
                data = { date: value };
            if (flag === 0)
                data = { time: value };
            this.setState(state => (Object.assign({}, state, { data: Object.assign({}, state.data, data), isOpened: false })), () => console.log(this.state, '123'));
        };
        this.handleStyleChange = (style, e) => {
            e.stopPropagation();
            this.setState(state => (Object.assign({}, state, { isOpened: false, data: Object.assign({}, state.data, { style }) })), () => console.log(this.state, 123));
        };
        this.handleStyleNumberChange = (number, e) => {
            e.stopPropagation();
            // replace(/[^0-9]/ig,"");
            this.setState(state => (Object.assign({}, state, { isOpened: false, data: Object.assign({}, state.data, { number }) })), () => console.log(this.state, 123));
        };
    }
    componentWillReceiveProps(nextProps) {
        console.log(this.props, nextProps);
    }
    componentWillUnmount() { }
    componentDidShow() { }
    componentDidHide() { }
    render() {
        return (<components_1.View className='Goods'>
        <taro_ui_1.AtToast isOpened={this.state.isToastOpened} text='正在开发中...'/>
        <components_1.View className='GoodsGroup'>
          <components_1.View className='GoodsImageUpload' onClick={this.handleImageUpload}>
            <components_1.Image src={this.state.data.GoodsPrizes[0].cover
            ?
                IMAGE_PREFIX + this.state.data.GoodsPrizes[0].cover
            :
                'https://via.placeholder.com/350x150'} mode='widthFix' className='image'/>
          </components_1.View>
          <taro_ui_1.AtForm>
            <taro_ui_1.AtInput name='value1' title='奖品名称' type='text' placeholder='请输入奖品名称' value={this.state.title} onChange={this.handleInputChange}/>
            <components_1.View className='GoodsInputNumber'>
              <components_1.View className='GoodsInputNumberLabel'>
                <components_1.Text className='text'>奖品数量</components_1.Text>
              </components_1.View>
              <taro_ui_1.AtInputNumber className='GoodsInputNumberValue' min={1} max={100} step={1} value={this.state.data.GoodsPrizes[0].number} onChange={this.handleNumberChange}/>
            </components_1.View>
          </taro_ui_1.AtForm>
        </components_1.View>
        
        <components_1.View className='GoodsDivider'>
          <components_1.Text className='title'>抽奖说明</components_1.Text>
          <components_1.Text className='subtitle'>（抽奖说明）</components_1.Text>
        </components_1.View>
        <components_1.View className='GoodsTextarea'>
          <taro_ui_1.AtTextarea value={this.state.explain} onChange={this.handleTextareaChange} maxlength='200' placeholder='填写本次抽奖说明'/>
        </components_1.View>
        <components_1.View className='GoodsDivider'>
          <components_1.Text className='title'>图文介绍</components_1.Text>
          <components_1.Text className='subtitle'>（非必填）</components_1.Text>
        </components_1.View>
        <taro_ui_1.AtList>
          <taro_ui_1.AtListItem title='开奖条件' arrow='right' extraText={this.state.data.style === 0 ? '按时间自动开奖' : this.state.data.style === 1 ? '按人数自动开奖' : '请选择开奖条件'} onClick={this.handleActionSheetSelect}/>
          <taro_ui_1.AtActionSheet isOpened={this.state.isOpened} cancelText='取消'>
            <taro_ui_1.AtActionSheetItem onClick={this.handleStyleChange.bind(this, 0)}>
              按时间自动开奖
            </taro_ui_1.AtActionSheetItem>
            <taro_ui_1.AtActionSheetItem onClick={this.handleStyleChange.bind(this, 1)}>
              按人数自动开奖
            </taro_ui_1.AtActionSheetItem>
          </taro_ui_1.AtActionSheet>
          {this.state.data.style === 0
            ?
                <components_1.View>
                <components_1.Picker mode='date' range={this.state.date} onChange={this.handleDateTimeChange.bind(this, 1)}>
                  <taro_ui_1.AtListItem title='开奖日期' arrow='right' extraText={this.state.data.date}/>
                </components_1.Picker>
                <components_1.Picker mode='time' range={this.state.time} onChange={this.handleDateTimeChange.bind(this, 0)}>
                  <taro_ui_1.AtListItem title='开奖时间' arrow='right' extraText={this.state.data.time}/>
                </components_1.Picker>
              </components_1.View>
            :
                this.state.data.style === 1
                    ?
                        <taro_ui_1.AtForm style={{ marginTop: -1 }}>
                  <taro_ui_1.AtInput name='number' type='text' placeholder='请输入按人数开奖的人数' onChange={this.handleStyleNumberChange}/>
                </taro_ui_1.AtForm>
                    :
                        null}
        </taro_ui_1.AtList>
        <components_1.View className='GoodsButtonFixed'>
          <taro_ui_1.AtButton type='primary' onClick={this.handleSubmits}>发起抽奖</taro_ui_1.AtButton>
        </components_1.View>
      </components_1.View>);
    }
};
Activity = __decorate([
    redux_1.connect(({ goods }) => ({
        goods
    }), (dispatch) => ({
        add(data, callback) {
            dispatch(goods_1.add(data, callback));
        }
    }))
], Activity);
exports.default = Activity;
//# sourceMappingURL=index.jsx.map